﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Entities;
using Microsoft.Xna.Framework;

namespace GameEngine.EntityComponents
{
    public class TerminateableComponent : IEntityComponent
    {
        AbstractEntity entity;
        private float healthPoints;
        private float maxHealthPoints;

        /// <summary>
        /// Initializes a new instance of the <see cref="TerminateableComponent" /> class.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public TerminateableComponent(AbstractEntity entity)
        {
            this.entity = entity;
            this.healthPoints = 100f;
            this.maxHealthPoints = healthPoints;
        }

        public void Update(GameTime gameTime)
        {
            
        }

        public void CollideWith(AbstractEntity target, bool isX, bool isY)
        { }

        #region Health

        /// <summary>
        /// Deals damage.
        /// </summary>
        /// <param name="damage">The damage.</param>
        public void DealDamage(float damage)
        {
            this.healthPoints -= damage;
            if (healthPoints < 0)
            {
                MoveableComponent moveable = (MoveableComponent)entity.GetComponent(typeof(MoveableComponent));
                if (moveable != null)
                    moveable.movementState = MoveableComponent.State.Dead;
            }
        }

        /// <summary>
        /// Gets the health percentage.
        /// </summary>
        /// <returns></returns>
        public float GetHealthPercentage()
        {
            return healthPoints / maxHealthPoints;
        }

        /// <summary>
        /// Determines whether the entity with this component is alive.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance is alive; otherwise, <c>false</c>.
        /// </returns>
        public bool IsAlive()
        {
            return (healthPoints >= 0);
        }

        /// <summary>
        /// Restores the health.
        /// </summary>
        /// <param name="healthRestored">The health restored.</param>
        public void RestoreHealth(float healthRestored)
        {
            this.healthPoints += healthRestored;
            if (this.healthPoints > this.maxHealthPoints) this.healthPoints = this.maxHealthPoints;
        }

        #endregion
    }
}
